package jui.maps
{
	import com.hurlant.crypto.tls.*;
	import com.seesmic.as3.xmpp.*;
	
	import flash.events.EventDispatcher;
	import flash.events.IEventDispatcher;
	
	import jui.business.ConnectPaneManager;
	import jui.business.ManagersFactory;
	import jui.business.XMPPManager;
	import jui.model.Config;
	
	import mx.binding.utils.BindingUtils;
	import mx.events.FlexEvent;
 	

	public class XMPPEventMap extends EventDispatcher implements IJUIEventMap
	{
		private var app: JUI;
		
		private var xmppManager: XMPPManager;
		private var connectPaneManager: ConnectPaneManager;		
		private var c: Config = Config.getConfig();
		
		public function setManagers(): void
		{
			var gm: Function = ManagersFactory.getInstance().getManager;
			
			xmppManager = getManager(XMPPManager.getQName()) as XMPPManager;
			connectPaneManager = getManager(ConnectPaneManager.getQName()) as ConnectPaneManager;
		}
			
		/**
		 * Event Handler for user click on 'Send' Button or Hit Ctrl+Enter
		 */
		public function setHandlers(): void
		{
			app.systemManager.addEventListener(FlexEvent.APPLICATION_COMPLETE, applicationCompleteHandler);
		}
		
		//----------------------------------------------------------------------
		// Event Handlers go gere
		//----------------------------------------------------------------------
		
		private function applicationCompleteHandler(event: FlexEvent): void
		{
			// Set JID in UI
			BindingUtils.bindProperty(app.connectionPane.jidInput, "text", connectPaneManager, "jid" );
			BindingUtils.bindProperty(app.connectionPane.passInput, "text", connectPaneManager, "pss" );
			
			// Set message output to UI
			// Halo 
			BindingUtils.bindProperty(app.messagePane.messageOutput, "htmlText", xmppManager, "messageOutput" );
			// Spark BindingUtils.bindProperty(app.messagePane.messageOutput, "content", xmppManager, "messageOutput" );
			
			// Set eventOutput to UI
			BindingUtils.bindProperty(app.statusPane.eventOutput, "text", xmppManager, "eventOutput" );
			
			// Set roster output ti UI
			BindingUtils.bindProperty(app.rosterPane, "rosterDataProvider", xmppManager, "rosterData" );
			
			// Set Respondent name in UI from Roster 
			BindingUtils.bindSetter(app.messagePane.messageInput.setStateFromRoster, xmppManager, "activeRespondentJID");
			
			// Set Respondent Status name in UI from Roster 
			BindingUtils.bindSetter(app.statusPane.setStatusFromRoster, xmppManager, "activeRespondentStatusText");
			
			setCredentials();
			
			if( c.autoConnectAtStart )
			{
				xmppManager.connect();
			}
		}

		private function setCredentials(): void
		{
			// Set JID in connection and UI 
			xmppManager.jid = connectPaneManager.jid = c.jid; 
			xmppManager.pss = connectPaneManager.pss = c.pss;
		}
		
		public function XMPPEventMap(target:IEventDispatcher=null, app: JUI=null): void
 		{
			if(instance) throw new Error("XMPPEventMap is singleton");
			this.app = app;
			setManagers();
			setHandlers();
 		}
		
		public static var instance: XMPPEventMap;
	}
}